package com.yhwch.fun.service;

import com.yhwch.fun.dao.TaskExecDAO;
import com.yhwch.fun.domain.TaskExecDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class TaskExecService {
    @Autowired
    private TaskExecDAO dao;

    private Map<String,String> execTypeMap = new ConcurrentHashMap<>();

    public boolean isExecute(String typeName,String batchNo){
        TaskExecDO p = new TaskExecDO();
        p.setTypeName(typeName);
        p.setBatchNo(batchNo);
        TaskExecDO taskExecDO = dao.select(p);
        if(taskExecDO == null){
            try {
                dao.insert(p);
                return true;
            }catch (DuplicateKeyException dup){
                System.out.println("任务已被抢先执行");
                return false;
            }catch (Exception e){
                System.out.println("其他异常不能执行,"+e.getMessage());
                return false;
            }
        }
        return false;
    }
    public void setExecTypeMap(String typeName,String type){
        execTypeMap.put(typeName,type);
    }

    public String getExecType(String typeName){
        if(execTypeMap.containsKey(typeName)){
            return execTypeMap.get(typeName);
        }
        return "insert";
    }
}
